package edu.ccut.saturn.web.filter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.saturn.core.impl.SaturnPropertiesService;

public class SessionTimeOutFilter implements Filter {
	static List<String> adminBundleList = null; 
	private FilterConfig config = null;
	static{
		initAdminBundleList();
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		Object userSaturnData = httpRequest.getSession().getAttribute(
				"__SATURN_AUTORITY_USER__"); 
		String loginAction = ((HttpServletRequest) request).getRequestURI();
		loginAction = loginAction.substring(1);
		loginAction = loginAction.substring(loginAction.indexOf("/"));
		
		if (userSaturnData == null
				&& !"/edu.ccut.saturn.authority.impl::/loginModule/action/Login.action"
						.equals(loginAction)
				&& !"/edu.ccut.saturn.authority.impl::/loginModule/action/LoginJson.action"
						.equals(loginAction)
				&& !"/edu.ccut.saturn.manager.bundles.impl::/bundleModule/releaseAction/ReleaseBundle.action"
				        .equals(loginAction)){
            if(gotoadmin(loginAction)){
            	request.getRequestDispatcher("/admin/web/login.jsp").forward(httpRequest,
            			httpResponse);
            }else{
            	request.getRequestDispatcher("/login.jsp").forward(httpRequest,
            			httpResponse);
            }
		} else {
			chain.doFilter(request, response);
		}
	}

	private boolean gotoadmin(String loginAction) {
		String bundleName = null;
		int endIndex = loginAction.indexOf("::");
		if(endIndex<0){
			bundleName = loginAction.split("/")[1];
		}else{
			 bundleName = loginAction.substring(1,loginAction.indexOf("::"));
		}
		return adminBundleList.contains(bundleName);
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}
    static private void initAdminBundleList(){
    	Properties properties = new Properties();
		InputStream in = null;

		try {
			in = SessionTimeOutFilter.class.getResourceAsStream("/adminbundle.properties");

			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		adminBundleList = new ArrayList<String>();
		for (Enumeration<?> propertyNames = properties.propertyNames(); propertyNames
				.hasMoreElements();) {
			String key = (String) propertyNames.nextElement();
			adminBundleList.add(key);
		}
    }
}
